package com.news.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.news.dao.NewsCommentDao;
import com.news.dao.NewsDetailDao;
import com.news.entity.NewsDetail;
import com.news.service.NewsDetailService;

@Service
public class NewsDetailServiceImpl implements NewsDetailService{

	@Autowired
	private NewsDetailDao newsDetailDao;
	@Autowired
	private NewsCommentDao newsCommentDao;
	
	public List<NewsDetail> getNewsByTitle(String title, int pageSize, int startIndex) {
		return newsDetailDao.getNewsByTitle(title, pageSize, startIndex);
	}

	public int getNewsCount(String title) {
		return newsDetailDao.getNewsCount(title);
	}
	
	//加事务的原因是因为两个删除在一个方法里面实现
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteNews(int id) {
		//删除当前新闻的所以评论
		newsCommentDao.deleteByNewsId(id);
		//删除新闻
		newsDetailDao.deleteNews(id);
		
	}

	public List<NewsDetail> getAllNews() {
		return newsDetailDao.getAllNews();
	}

	public NewsDetail getByIdNews(int id) {
		return newsDetailDao.getByIdNews(id);
	}

	public void addNews(NewsDetail newsDetail) {
		newsDetailDao.addNews(newsDetail);
	}
}
